<?php

class SinhVienDsDeTaiLV extends BaseController {

    public function index() {
        return View::make('sinhvien.dsdetai')->with('title', 'Danh sách đề tài luận văn');
    }

    public function getDanhSachDeTai($idbm, $hk = null, $nk = null) {
        if ($hk == null && $nk == null) {
            $idhknk = HocKyNienKhoa::max('id');
        } else {
            $idhknk_array = $this->getIdHkNk($hk, $nk);
            if (count($idhknk_array) != 0) {
                $idhknk = $idhknk_array[0]->id;
            } else {
                $idhknk = HocKyNienKhoa::max('id')+1;
            }
        }
        $de_tais = DeTaiLuanVan::select('*')
                ->where('id_hoc_ki_nien_khoa', $idhknk, 'and')
                ->where('id_bo_mon', $idbm, 'and')
                ->where('trang_thai', 1)
                ->orderBy('ma_so_de_tai')
                ->paginate(DE_TAI_PERPAGE);
        return $de_tais;
    }
    private function getIdHkNk($hk, $nk) {
        $idhknk = HocKyNienKhoa::select('id')
                ->where('hoc_ky', $hk, 'and')
                ->where('nien_khoa', $nk)
                ->get();
        return $idhknk;
    }
    public function lay_ms_nhom($iddetai) 
    {
        $array = DB::table('nhom_dang_ki')
                ->select('ms_nhom')
                ->where('id_de_tai','=',$iddetai)
                ->first();
        return $array;
    }
	public function lay_ds_nhom_dang_ky($iddetai) 
    {
        $array = DB::table('nhom_dang_ki')
                ->select('id')
                ->where('id_de_tai','=',$iddetai)
                ->get();
        return $array;
    }
    public function lay_id_nhom($idsinhvien) 
    {
        $array = DB::table('nhom_dang_ki')
                ->select('id','id_de_tai','ms_nhom','trang_thai_th')
                ->where('id_sinh_vien','=',$idsinhvien)
                ->first();
        return $array;
    }
    public function lay_thong_tin_de_tai($iddetai) 
    {
        $array = DB::table('de_tai_luan_van')
                ->select('ten_de_tai')
                ->where('id','=',$iddetai)
                ->first();
        return $array;
    }
    public function lay_id_chi_tiet_can_bo_huong_dan($iddetai) 
    {
        $array = DB::table('chi_tiet_can_bo_huong_dan')
                ->select('id')
                ->where('id_de_tai','=',$iddetai,'and')
                ->where('stt_vai_tro','=','1')
                ->first();
        return $array;
    }
    public function dang_ky_de_tai()
    {
        //them nhom dang ky
        $idsinhvien = Input::get('txtIDSinhVien');
        $iddetai = Input::get('txtIDDeTai');
        if ($this->lay_ms_nhom($iddetai))
        {
            $msnhom = $this->lay_ms_nhom($iddetai)->ms_nhom;
        }
        else 
        {
            $msnhom = "nhomdk".$iddetai;
        }
        $ngaygio = date("Y-m-d h:i:s", time());
        $idchitietcanbohuongdan = $this->lay_id_chi_tiet_can_bo_huong_dan($iddetai)->id;
        $array  = DB::table('nhom_dang_ki')
            ->insert(array(
                'id_sinh_vien' => $idsinhvien,
                'id_de_tai' => $iddetai,
                'ms_nhom' => $msnhom,
                'tg_dang_ki' => $ngaygio)); 
        //them danh gia sinh vien
        return Redirect::back()->with('ketquadangky', $array);             
    }
    public function huy_dang_ky_de_tai()
    {
        $idsinhvien = Input::get('txtIDSinhVien');
        $iddetai = Input::get('txtIDDeTai');
        $array  = 
        	DB::table('nhom_dang_ki')
				->where('id_sinh_vien','=',$idsinhvien)            
            	->delete(); 
        return Redirect::back()->with('ketquahuydangky', $array);             
    }
    public function xin_bao_luu()
    {
        $idsinhvien = Input::get('txtIDSinhVien');
        $ngaygio = date("Y-m-d h:i:s", time());
        $iddetai = Input::get('txtIDDeTai');
        $array = 
        	DB::table('nhom_dang_ki')
				->where('id_sinh_vien','=',$idsinhvien,'and')
				->where('id_de_tai','=',$iddetai)            
            	->update(array('trang_thai_th' => '0',
            					'tg_xin_bao_luu' => $ngaygio)); 
        return Redirect::back()->with('ketquaxinbaoluu', $array);             
    }
}
